const BehaviorAll = require("../../../behaviors/behavior_all.js");
const Validate = require("../add/validate.js");
const ServiceLogin = require("../../../service/service_login.js");
const Tool = require("../../../utils/tool.js");
const ServiceRequest = require("../../../service/service_request.js");
const App = getApp();

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    validate: null,
    hasRequestAgent: false,
    tab:1 // 1：注册，2：登录
  },

  methods: {
    onUnload: function(){
      App.globalData.isLogin = false;
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      _this.validate = Validate.createInstance();
      // 判断是否已经申请代理
      ServiceRequest.hasRequestAgent(function (data) {
        if(data === true){
          _this.setData({ hasRequestAgent: true})
        }else{
          _this.setData({ hasRequestAgent: false })
        }
      });
    },

    onTab: function(e){
      const _this = this;
      const tab = Number(e.currentTarget.dataset.tab);
      _this.setData({ tab })
    },

    onRequest: function(e){
      const _this = this;
      /*
       * 表单提交校验
       */
      const params = e.detail.value
      if (!_this.validate.checkForm(params)) {
        wx.showModal({
          content: _this.validate.errorList[0].msg,
          showCancel: false,
        });
        return false
      }
      /**
       * 处理提交
       */
      ServiceRequest.requestAgent(params, function(data){
        _this.setData({ hasRequestAgent: true })
      });
    },

    onLogin: function (e) {
      const _this = this;
      const value = e.detail.value;
      ServiceLogin.testLogin(value.name, value.password, function (data) {
        App.globalData.isLogin = true;
        App.globalData.user = data.user;
        // _this._success();
        wx.reLaunch({ url: '/pages/index/index' });
      });
    },
    /**
     * 拨打电话
     */
    onCall: function (e) {
      wx.makePhoneCall({
        phoneNumber: '400 859 2759',
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
    
    handleContact: function (e) {
      console.log(e.detail.path)
      console.log(e.detail.query)
    }
  }

})